﻿using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Client.Forms
{
    public partial class VoiceSettingForm : Form
    {
        public VoiceSettingForm()
        {
            InitializeComponent();
        }

        private void VoiceSettingForm_Load(object sender, EventArgs e)
        {
            string pattern = @"^中文-简体: .+(.+)";
            var names = Managers.Manager.ttsM.GetVoiceNames();
            int i = 0;
            foreach(string name in names)
            {
                if (name != "" && !Regex.IsMatch(name,pattern))
                {
                    comboBox1.Items.Add(name);
                    if (name == Managers.Manager.ttsM.GetVoiceName())
                        comboBox1.SelectedIndex = i;
                    i++;
                }
            }
            speedNumericUpDown.Value = Managers.Manager.ttsM.GetVoiceSpeed();
            pitchNumericUpDown.Value = Managers.Manager.ttsM.GetVoicePitch();
            volumeNumericUpDown.Value = Managers.Manager.ttsM.GetVoiceVolume();

            var st = Managers.Manager.cronM.indexStocks[0];
            textBox1.Text = st.name + st.trade + "点";
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string name = (string)comboBox1.Items[comboBox1.SelectedIndex];
            Managers.Manager.ttsM.SetVoice(name);
            speedNumericUpDown.Value = Managers.Manager.ttsM.GetVoiceSpeed();
            pitchNumericUpDown.Value = Managers.Manager.ttsM.GetVoicePitch();
            volumeNumericUpDown.Value = Managers.Manager.ttsM.GetVoiceVolume();

            Managers.Manager.ttsM.Speak(textBox1.Text);
        }

        private void speedNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            Managers.Manager.ttsM.SetVoiceSpeed((uint)speedNumericUpDown.Value);
            Managers.Manager.ttsM.Speak(textBox1.Text);
        }

        private void pitchNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            Managers.Manager.ttsM.SetVoicePitch((uint)pitchNumericUpDown.Value);
            Managers.Manager.ttsM.Speak(textBox1.Text);
        }

        private void volumeNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            Managers.Manager.ttsM.SetVoiceVolume((uint)volumeNumericUpDown.Value);
            Managers.Manager.ttsM.Speak(textBox1.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
